package event;

import mail.SndMail;
import type.Receiver;
import type.Report;
import utility.CapitalChar;
import utility.Input;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.SearchRecord;
import java.io.File;
import java.util.*;


public class Sendmail extends Thread implements Event
{
	private boolean Result=false; 
	String emailid;
	String msg;
	String tid;
	String transaction_type;
	String TRANSTYPE="transaction_type";
	String REPORT="report_master";
	GetIdName gid;
	DBManager db;
	SearchRecord sr; 
	String userId;
	Hashtable<Object, Object> Sendmail = new Hashtable<Object, Object>();
	Hashtable<Object, Object> Reportmap = new Hashtable<Object, Object>();
	Vector<Object> RECEIVERemailid =new Vector<Object>();
	String Source;
	String attachment;
	
	public Sendmail()
	{
	}
	 
	public static void main(String hmt[])
	{
		try
		{
			DBManager db = new DBManager();
			db.getConnect();
			Sendmail sm = new Sendmail();
			sm.SetData("o<aa,ada10#605", "po", "9", "+919922930640");
			sm.initializationData(db);
			sm.setObject("");
			System.out.println("Email Id : "+sm.getUserEmail("+919922930640"));
			sm.run();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void SetData(String msg, String transaction_type, String tid, String userid,String Source)
	{	
		this.msg =msg;
		this.transaction_type=transaction_type;
		this.tid=tid;
		this.userId = userid;
		this.Source = Source;
	}
	
	public void run() 
	{	
		
		try 
		{	
			String subject =Sendmail.get("subject").toString() + " "+ tid;
			String bodyText = Sendmail.get("bodytext").toString();
			if(REPORT.toLowerCase().equals(Source.toLowerCase()))
			{
				Report report = new Report(db,gid.getId(msg));
				String typeofreport = report.GettypeofReport();
				query.condition.typereport.Typereport type; 
				Class C = Class.forName("query.condition.typereport."+CapitalChar.makeFirstCharCapital(typeofreport.trim()));
				type = (query.condition.typereport.Typereport )C.newInstance();
				type.setData("",tid, db, Reportmap);
				String path = type.getPath(msg);
				attachment=path;
			}
			else
			{	
				SndMail send = new SndMail();
					
						try
						{	
							send.postMail("bills@osc.co.in" ,RECEIVERemailid,attachment,subject,bodyText);
						}
						catch (Exception e) 
						{
						}
					Result = true;
				}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public void setObject(String eventobject) 
	{
		try 
		{
			int indexOfPass = msg.indexOf('#');
			msg = msg.substring(0, indexOfPass);
		}
		catch (Exception e1)
		{
			
			e1.printStackTrace();
		}
		String Tra_path = Input.TRASACTIONREPORTSPATH +TRANSTYPE+"//"+gid.getId(transaction_type) + Input.FILRFORMAT;
		String path = Input.MASTERREPORTSPATH + eventobject + "//" +eventobject +"_mid" + Input.FILRFORMAT;
		Hashtable< Object, Object> htable = new Hashtable<Object, Object>();
		sr =new SearchRecord(path);
		htable.clear();
		htable.put("mid", eventobject);
		String line[] = sr.getRecordSet(htable);
		htable.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value= splitline[Column.pv_index-1];
			if(value.equals("0"))
				value =  splitline[Column.vt_index-1];
			else
				value = gid.getItem(value);
			Sendmail.put(gid.getItem(splitline[Column.pid_index-1]), value);
		}
		attachment=Input.ROOT+Sendmail.get("attachment").toString()+tid+"_mail"+Input.FILRFORMAT;
		File file =new File(attachment);
		if(file.exists()==true)
		{
			try 
			{
				htable.put("id",tid);
				sr = new SearchRecord(Tra_path);
				String line_tra_file []= sr.getRecordSet(htable);
				Receiver re =new Receiver( path,transaction_type,db,"Emailid");
				RECEIVERemailid = re.findReceiver(line_tra_file[0]);
				String UserEmail = getUserEmail(userId); 
				RECEIVERemailid.add(UserEmail);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			run();
		}
		else
			Result = false;
	}
	public void initializationData(DBManager database) 
	{
		this.db=database;
		gid = new GetIdName(db);
		REPORT = gid.getId(REPORT);
		TRANSTYPE =gid.getId(TRANSTYPE);
	}

	public boolean getResult() 
	{
		return Result;
		//return true; // THIS LINE ADD -> INTENET IS NOT WORKING 
	}
	String getUserEmail(String mobileNo)
	{
		try
		{
			String no = mobileNo;
			int mid_index=-1;
			int pv_index=-1;
			int vt_index =-1;
			mobileNo = gid.getId(mobileNo);
			String path = Input.MASTERREPORTSPATH + mobileNo + "//" +mobileNo +"_PV" + Input.FILRFORMAT;
			Hashtable<Object, Object> table = new Hashtable<Object, Object>();
			table.put("pv",mobileNo); 
			table.put("pid", gid.getId("mobileno"));
			SearchRecord sr = new SearchRecord(path);
			String [] result = sr.getRecordSet(table);
			if(result.length == 0)
			{
				path = Input.MASTERREPORTSPATH + no + "//" +no +"_VT" + Input.FILRFORMAT;
				table.remove("pv");
				table.put("vt",no);
				sr = new SearchRecord(path);
				result = sr.getRecordSet(table);
			}
			try 
			{
				mid_index = Column.mid_index-1;
				pv_index = Column.pv_index-1;
				vt_index = Column.vt_index-1;
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			String [] midResult = result[0].split("\t");
			String name = midResult[mid_index];
			path = Input.MASTERREPORTSPATH + name + "//" +name +"_mid" + Input.FILRFORMAT;
			table.clear();
			table.put("pid", gid.getId("emailid"));
			sr = new SearchRecord(path);
			String result1[] = sr.getRecordSet(table);
			midResult = result1[0].split("\t");
			name = midResult[pv_index];
			if(name.equals("0"))
			{
				name = midResult[vt_index];
			}
			else
				name = gid.getId(name); 
			return name;
		}
		catch (RuntimeException e) 
		{
			e.printStackTrace();
		}
		
		return " ";
	}

	public void SetData(String msg, String transaction_type, String tid, String userid) 
	{
		this.msg =msg;
		this.transaction_type=transaction_type;
		this.tid=tid;
		this.userId = userid;
	}

	public boolean afterAdd() 
	{
		return getResult();
	}

	public boolean afterDel() 
	{
		return getResult();
	}
	
	public boolean afterMod() 
	{
		return getResult();
	}

	public boolean afterView() 
	{
		return getResult();
	}
}
		